﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unis.Framework.Core.Utility;

using KingRoad.EPM.CompletionMaterialsEntity.Model;
using KingRoad.EPM.CompletionMaterialsDAL.BcdFileDataService;

namespace KingRoad.EPM.CompletionMaterialsBLL.UIRules
{
    /// <summary> 
    /// 归档材料类型表
    /// 文件生成时间 2017/5/12 9:27:04
    /// </summary> 
    public class BcdFiledataUIRule
    {
        private readonly IBcdFiledataService _bcdFiledataService;


        public BcdFiledataUIRule() { }

        /// <summary> 
        /// construct function... 
        /// </summary> 
        public BcdFiledataUIRule(IBcdFiledataService bcdFiledataService)
        {
            _bcdFiledataService = bcdFiledataService;
        }

        /// <summary>
        /// 获取所有归档材料类型
        /// </summary>
        /// <returns></returns>
        public List<BcdFiledataInfo> GetEntityList()
        {
            var ret = _bcdFiledataService.GetEntityList();
            return ret;
        }
        /// <summary>
        /// 获取所有归档材料类型
        /// </summary>
        /// <returns></returns>
        public List<BcdFiledataInfo> GetEntityListBySql(string sql)
        {
            var ret = _bcdFiledataService.GetEntityList(sql);
            return ret;
        }
        /// <summary>
        /// 根据父id获取子类型条数
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public int GetCountByFId(string fid)
        {
            return _bcdFiledataService.GetCountByFId(fid);
        }
        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public BcdFiledataInfo GetEntityById(Guid id)
        {
            var ret = _bcdFiledataService.GetEntityById(id);
            return ret;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertEntity(BcdFiledataInfo entity)
        {
            var ret = _bcdFiledataService.InsertEntity(entity);
            return ret > 0;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateEntity(BcdFiledataInfo entity)
        {
            var ret = _bcdFiledataService.UpdateEntity(entity);
            return ret > 0;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteEntity(string id)
        {
            var ret = _bcdFiledataService.DeleteEntityByStringID(id);
            return ret > 0;
        }
        /// <summary>
        /// 更新父集合ID
        /// </summary>
        /// <returns></returns>
        public bool UpdateParentIds(string pid)
        {
            BcdFiledataInfo pInfo = new BcdFiledataInfo();
            string sql = "";
            if (pid == null)
                sql = " Fd_PID is null ";
            else
            {
                sql = " Fd_PID='" + pid + "' ";
                pInfo = _bcdFiledataService.GetEntityById(new Guid(pid));
            }
            List<BcdFiledataInfo> list = _bcdFiledataService.GetEntityList(sql);
            foreach (BcdFiledataInfo info in list)
            {
                if (pid != null)
                {
                    info.Fd_PIds = pInfo.Fd_PIds + ",'" + pid + "'";
                    _bcdFiledataService.UpdateEntity(info);
                }
                UpdateParentIds(info.Fd_ID);
            }
            return true;
        }

        /// <summary>
        /// 根据id获取 该类型下的所有子、孙、孙孙……类型id集合
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetAllIdsById(string id)
        {
            string ids = "";
            List<BcdFiledataInfo> list = new List<BcdFiledataInfo>();
            list = _bcdFiledataService.GetEntityList(" Fd_PIds like '%" + id + "%' ");
            foreach (BcdFiledataInfo info in list)
            {
                ids +=  info.Fd_ID + ",";
            }
            return ids.Trim(',');
        }

        /// <summary>
        /// 根据id获取 id 的所有父类型id集合
        /// </summary>
        //// <param name="pId">弹出框的父类型</param>
        /// <param name="id">本次勾选的类型id</param>
        /// <returns></returns>
        public string GetAllPIdsById(string pId, string id)
        {
            BcdFiledataInfo info = _bcdFiledataService.GetEntityById(new Guid(id));
            string ids = info.Fd_PIds.Replace("'", "");
            ids = ids.Substring(ids.IndexOf(pId)).Replace(pId, "").Trim(',');
            return ids;
        }
    }
}

